<%--
  Created by IntelliJ IDEA.
  User: 25306
  Date: 2025/10/8
  Time: 下午3:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>backend</title>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/bootstarp/css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/index.css" />
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/nmms.css" />
    <script src="${pageContext.request.contextPath}/js/jquery.js" type="text/javascript" charset="utf-8"></script>
    <script src="${pageContext.request.contextPath}/bootstarp/js/bootstrap.js" type="text/javascript" charset="utf-8"></script>
    <script src="${pageContext.request.contextPath}/js/userSetting.js" type="text/javascript" charset="utf-8"></script>
    <script src="${pageContext.request.contextPath}/js/bootstrap-paginator.js" type="text/javascript" charset="utf-8"></script>
    <script>
        $(function () {
            // 分页
            $("#deptPageHelper").bootstrapPaginator({
                bootstrapMajorVersion: 3,   // 指定bootstrap版本,默认是3之前的版本
                currentPage: "${deptPage.pageNum == 0 ? 1 : deptPage.pageNum}",
                totalPages: "${deptPage.pages == 0 ? 1 : deptPage.pages}",
                size: "normal",
                alignment: "center",
                // 无参查询,当点击页码之后跳转的页面
                pageUrl: function (type, page, current) {
                    return "${pageContext.request.contextPath}/dept/findAll?pageNo=" + page;
                }
            })

            $(".addDept").click(function(){
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/dept/addDept",
                    data:{"name":$("#dept-name").val(),
                           "remark":$("#dept-remark").val()},
                    dataType: "json",
                    success: function (result) {
                        if (result.responseCode == "2001") {
                            console.log(result.message);
                            location.href = "${pageContext.request.contextPath}/dept/findAll?pageNo=${deptPage.pageNum == 0 ? 1 : deptPage.pageNum}";
                        } else {
                            $("#errorMsg").html(result.message);
                        }
                    }
                })
            })

            $(".doAddSonDept").click(function(){
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/dept/findDeptById",
                    data: {"id": $(this).attr("data-id")},
                    dataType: "json",
                    success: function (result) {
                        $("#father-name").val(result.returnObject.name);
                        $("#father-name").attr("data-id", result.returnObject.id);
                    }
                })
            })

            $(".addSonDept").click(function(){
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/dept/addSonDept",
                    data:{"name":$("#son-name").val(),
                        "remark":$("#son-remark").val(),
                        "fatherDeptId":$("#father-name").attr("data-id")},
                    dataType: "json",
                    success: function (result) {
                        if (result.responseCode == "2001") {
                            console.log(deptPage.pageNum);
                            location.href = "${pageContext.request.contextPath}/dept/findAll?pageNo=${deptPage.pageNum == 0 ? 1 : deptPage.pageNum}";
                        } else {
                            $("#errorMsg").html(result.message);
                        }
                    }
                })
            })
            $(".doModifyDept").click(function(){
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/dept/findDeptById",
                    data: {"id": $(this).attr("data-id")},
                    dataType: "json",
                    success: function (result) {
                        $("#modify-id").val(result.returnObject.id);
                        $("#modify-name").val(result.returnObject.name);
                        $("#modify-remark").val(result.returnObject.remark);
                    }
                })
            })

            $(".modifyDept").click(function(){
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/dept/updateDept",
                    data: {"id": $("#modify-id").val(),
                        "name": $("#modify-name").val(),
                        "remark": $("#modify-remark").val()},
                    dataType: "json",
                    success: function (result) {
                        if (result.responseCode == "2001") {
                            location.href = "${pageContext.request.contextPath}/dept/findAll?pageNo=${deptPage.pageNum == 0 ? 1 : deptPage.pageNum}";
                        } else {
                            $("#errorMsg").html(result.message);
                        }
                    }
                })
            })
            $(".enableDept").click(function(){
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/dept/enableDept",
                    data: {"id": $(this).attr("data-id")},
                    dataType: "json",
                    success: function (result) {
                        if (result.responseCode == "2001") {
                            location.href = "${pageContext.request.contextPath}/dept/findAll?pageNo=${deptPage.pageNum == 0 ? 1 : deptPage.pageNum}";
                        } else {
                            $("#errorMsg").html(result.message);
                        }
                    }
                })
            })
            $(".disableDept").click(function(){
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/dept/disableDept",
                    data: {"id": $(this).attr("data-id")},
                    dataType: "json",
                    success: function (result) {
                        if (result.responseCode == "2001") {
                            location.href = "${pageContext.request.contextPath}/dept/findAll?pageNo=${deptPage.pageNum == 0 ? 1 : deptPage.pageNum}";
                        } else {
                            $("#errorMsg").html(result.message);
                        }
                    }
                })
            })
        });
    </script>
</head>

<body>

<!-- 部门管理 -->
<div class="panel panel-default" id="departmentSet">
    <div class="panel-heading">
        <h3 class="panel-title">部门管理&nbsp;&nbsp;<span id="errorMsg" style="color: red;"></span></h3>
    </div>
    <div class="panel-body">
        <input type="button" value="添加部门" class="btn btn-primary" id="doAddDept">
        <br>
        <br>
        <div class="modal fade" tabindex="-1" id="Dept">
            <!-- 窗口声明 -->
            <div class="modal-dialog modal-lg">
                <!-- 内容声明 -->
                <div class="modal-content">
                    <!-- 头部、主体、脚注 -->
                    <div class="modal-header">
                        <button class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">添加部门</h4>
                    </div>
                    <div class="modal-body text-center">
                        <div class="row text-right">
                            <label for="dept-name" class="col-sm-4 control-label">部门名称：</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="dept-name">
                            </div>
                        </div>
                        <br>
                        <div class="row text-right">
                            <label for="dept-remark" class="col-sm-4 control-label">部门职能：</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="dept-remark">
                            </div>
                        </div>
                        <br>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-primary addDept">添加</button>
                        <button class="btn btn-primary cancel" data-dismiss="modal">取消</button>
                    </div>
                </div>
            </div>
        </div>

        <!-- 添加子部门模态框 -->
        <div class="modal fade" tabindex="-1" id="sonDept">
            <!-- 窗口声明 -->
            <div class="modal-dialog modal-lg">
                <!-- 内容声明 -->
                <div class="modal-content">
                    <!-- 头部、主体、脚注 -->
                    <div class="modal-header">
                        <button class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">添加子部门</h4>
                    </div>
                    <div class="modal-body text-center">
                        <div class="row text-right">
                            <label for="father-name" class="col-sm-4 control-label">父部门名称：</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="father-name"  readonly="readonly">
                            </div>
                        </div>
                        <br>
                        <div class="row text-right">
                            <label for="son-name" class="col-sm-4 control-label">部门名称：</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="son-name">
                            </div>
                        </div>
                        <br>
                        <div class="row text-right">
                            <label for="son-remark" class="col-sm-4 control-label">部门职能：</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="son-remark">
                            </div>
                        </div>
                        <br>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-primary addSonDept">添加</button>
                        <button class="btn btn-primary cancel" data-dismiss="modal">取消</button>
                    </div>
                </div>
            </div>
        </div>


        <!-- 修改部门模态框 -->
        <div class="modal fade" tabindex="-1" id="modifyDept">
            <!-- 窗口声明 -->
            <div class="modal-dialog modal-lg">
                <!-- 内容声明 -->
                <div class="modal-content">
                    <!-- 头部、主体、脚注 -->
                    <div class="modal-header">
                        <button class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">修改部门</h4>
                    </div>
                    <div class="modal-body text-center">
                        <div class="row text-right">
                            <label for="modify-id" class="col-sm-4 control-label">编号：</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="modify-id" readonly="readonly">
                            </div>
                        </div>
                        <br>
                        <div class="row text-right">
                            <label for="modify-name" class="col-sm-4 control-label">部门名称：</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="modify-name">
                            </div>
                        </div>
                        <br>
                        <div class="row text-right">
                            <label for="modify-remark" class="col-sm-4 control-label">部门职能：</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="modify-remark">
                            </div>
                        </div>
                        <br>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-primary modifyDept">修改</button>
                        <button class="btn btn-primary cancel" data-dismiss="modal">取消</button>
                    </div>
                </div>
            </div>
        </div>

        <div class="show-list">
            <table class="table table-bordered table-hover" style='text-align: center;'>
                <thead>
                <tr class="text-danger">
                    <th class="text-center">序号</th>
                    <th class="text-center">部门编号</th>
                    <th class="text-center">部门名称</th>
                    <th class="text-center">部门职能</th>
                    <th class="text-center">所属部门</th>
                    <th class="text-center">部门状态</th>
                    <th class="text-center">操作</th>
                </tr>
                </thead>
                <tbody id="tb">
                <c:forEach items="${deptPage.list}" var="type">
                    <tr>
                        <td>${type.id}</td>
                        <td>${type.no}</td>
                        <td>${type.name}</td>
                        <td>${type.remark}</td>
                        <td>${type.fatherDeptId == null ? '--' : type.fatherDept.name}</td>
                        <td>
                            <c:if test="${type.status == 1}">有效</c:if>
                            <c:if test="${type.status == 0}">无效</c:if>
                        </td>
                        <td class="text-center">
                            <input type="button" class="btn btn-info btn-sm doAddSonDept" data-id="${type.id}" value="添加子部门">
                            <input type="button" class="btn btn-warning btn-sm doModifyDept" data-id="${type.id}" value="修改">
                            <c:if test="${type.status == 1}">
                                <input type="button" class="btn btn-danger btn-sm doDisable disableDept" data-id="${type.id}" data-status="${type.status}" value="禁用">
                            </c:if>
                            <c:if test="${type.status == 0}">
                                <input type="button" class="btn btn-success btn-sm doDisable enableDept" data-id="${type.id}" data-status="${type.status}" value="启用">
                            </c:if>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
            <ul id="deptPageHelper"></ul>
        </div>
    </div>
</div>
</body>

</html>
